package jjn.round2;

import commons.TreeNode;

/**
 * @author Jiang Jining
 * @since 2023-05-10 23:15
 */
public class Offer54_KthLargest {
    
    private int count = 0;
    private int result = 0;
    
    public int kthLargest(TreeNode root, int k) {
        helper(root, k);
        return result;
    }
    
    /**
     * 中序遍历
     *
     * @param node node
     * @param k    kth node - val : target
     */
    private void helper(TreeNode node, int k) {
        if (node == null) {
            return;
        }
        if (node.right != null) {
            helper(node.right, k);
        }
        if (++count == k) {
            result = node.val;
            return;
        }
        if (node.left != null) {
            helper(node.left, k);
        }
    }
}
